import wx, Tms, Printing

class frmReports(wx.Frame):
    """The Report window for printing reports""" 
    def __init__(self, parent, *args, **kwargs):
        wx.Frame.__init__(self, parent, *args, **kwargs)
        self.p = parent
        #self.Data = data
        self.Bind(wx.EVT_CLOSE, self.on_Report_close)

        self.ReportPanel = wx.Panel(self)
        self.btnBack = wx.Button(self.ReportPanel, label="<<<< Back", pos=(37,540))
        self.btnBack.Bind(wx.EVT_BUTTON, self.On_Back_btn)
        
        self.sttxtReportType = wx.StaticText(self.ReportPanel, pos=(10,10), label='Select the Type of Report You want to print:')
        self.cmbbxReportType = wx.ComboBox(self.ReportPanel, pos=(10, 30), choices=['Schedule', 'Assignment Slips', 'Worksheets'])
        self.cmbbxReportType.Bind(wx.EVT_COMBOBOX, self.on_cmbbxReportType_select)
        self.cmbbxReportType.SetSelection(0)
        self.sttxtDates = wx.StaticText(self.ReportPanel, pos=(10, 60), label="Choose Week(s) to Print:")
        self.treeDates = wx.TreeCtrl(self.ReportPanel, pos=(10, 80), size=(100,310), style=wx.TR_HIDE_ROOT|wx.TR_MULTIPLE)
        self.treeDates.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_treeDates_changed)
        self.sttxtSlips = wx.StaticText(self.ReportPanel, pos=(160, 60), label='Choose slips to print:')
        self.sttxtSlips.Show(False)
        self.treeSlips = wx.TreeCtrl(self.ReportPanel, pos=(160, 80), size=(400,310), style=wx.TR_HIDE_ROOT|wx.TR_MULTIPLE)
        self.treeSlips.Show(False)
        
        self.btnPrint = wx.Button(self.ReportPanel, pos=(700, 540), label='Print')
        self.btnPrint.Bind(wx.EVT_BUTTON, self.on_btnPrint_click)
    
        self.Center(wx.BOTH)
        Tms.loadTreeDates(self, self.treeDates)
        self.treeDates.UnselectAll()
        
    def on_treeDates_changed(self, event=None):
        try:
            choice = self.cmbbxReportType.GetStringSelection()
        except:
            event.Skip()
            return
        
        if choice == 'Assignment Slips':
            self.sttxtSlips.Show(True)
            self.treeSlips.Show(True)
            self.chooseAssignmentSlips()
        else:
            self.treeSlips.Show(False)
            self.sttxtSlips.Show(False)
        event.Skip()
        
    def chooseAssignmentSlips(self):
        #load tree with talks to print assignments slips for
        self.treeSlips.DeleteAllItems()
        root = self.treeSlips.AddRoot('Main')        
        dateID = self.treeDates.GetSelections()
        for id in dateID:
            # load talks for this date
            cal = self.p.db.Calendar
#            print cal
            date = self.treeDates.GetItemText(id)
            if cal[date]['Status'] != 'Cancelled':
                # cycle through each talk and add to treelist
                if cal[date]['H ID'] >= 0:
                    self.treeSlips.AppendItem(root, date + ' |   Highlights:  ' + Tms.GetStudentsName(self, cal[date]['H ID']))
    
    def on_cmbbxReportType_select(self, event=None):
        """ Report selected, show options """
        choice = self.cmbbxReportType.GetStringSelection()
        if (choice == 'Schedule') or (choice == 'Assignment Slips') or (choice == 'Worksheets'):
            self.sttxtDates.Show(True)
            self.treeDates.Show(True)
        else:
            self.treeDates.Show(False)
            self.sttxtDates.Show(False)
        event.Skip()
        
    def on_btnPrint_click(self, event=None):
        """ Gather Choices and print report """
        repType = self.cmbbxReportType.GetStringSelection()
        selections = self.treeDates.GetSelections()
        dates = []
        if len(selections) >0:
            for item in selections:
                dates.append(self.treeDates.GetItemText(item))
            
            Printing.buildReport(self, [repType, dates])
        event.Skip()
        
    def on_Report_close(self, event=None):
        self.frame.Enable(True)
        self.frame.SetFocus()
        if event == None:
            self.Close()
        else:
            event.Skip()
    
    def On_Back_btn(self, event=None):
        """back button pressed, so close report window"""
        self.on_Report_close()

if __name__ == '__main__':
    Tms.Main()
